//
//  TZMorePushController.m
//  wangYiLottery
//
//  Created by mac on 16/3/2.
//  Copyright © 2016年 tz. All rights reserved.
//

#import "TZMorePushController.h"
#import "UIView+frame.h"
#import "TZLiveModel.h"
#import "TZLiveCell.h"

@interface TZMorePushController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) NSArray *liveArray;
@end

@implementation TZMorePushController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.navigationItem.title = @"推送和提醒";
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.liveArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TZLiveCell *cell = [TZLiveCell settingWithTableView:tableView];
    cell.timerStr = @"00:00";
    cell.model = self.liveArray[indexPath.section];
    return cell;
}

#pragma mark - UITableViewDelegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    TZLiveModel *model = self.liveArray[section];
    
    return model.header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return;
    }
    //创建文本框
    UITextField *field = [[UITextField alloc] init];
    //创建datePicker
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    //设置显示模式
    datePicker.datePickerMode = UIDatePickerModeTime; //时 分
    //设置显示的文字
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"]; //中文
    self.datePicker = datePicker;
    
    //创建toolbar
    UIToolbar *bar = [[UIToolbar alloc] init];
    //创建toolbar上的按钮
    //取消
    UIBarButtonItem *closeView = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(closeButton)];
    //弹簧
    UIBarButtonItem *flysView = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    //完成
    UIBarButtonItem *doneView = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(doneButton)];
    //添加到toolbar上
    bar.items = @[closeView,flysView,doneView];
    bar.H = 44;
    
    //设置输入视图
    field.inputView = datePicker;
    //设置辅助视图
    field.inputAccessoryView = bar;
    //获取点击的cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //添加到cell上
    [cell.contentView addSubview:field];
    //让文本框成为第一响应者
    [field becomeFirstResponder];
}
//取消按钮的监听事件
- (void)closeButton {
    //退出编辑模式
    [self.view endEditing:YES];
}
//确定按钮的监听事件
- (void)doneButton {
    //获取当前选择的时间
    NSDate *date = self.datePicker.date;
    //创建格式化对象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"HH:mm";
    //转成string
    NSString *time = [formatter stringFromDate:date];
    //获取选中行的indexPath
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    //通过indexPath获取cell
    TZLiveCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    //设置subtitle
    //    cell.detailTextLabel.text = time;
    cell.timerStr = time;
    cell.model = self.liveArray[indexPath.section];
    //关闭键盘
    [self closeButton];
}

- (instancetype)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (NSArray *)liveArray {
    if (_liveArray == nil) {
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Live.plist" ofType:nil]];
        NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:tempArray.count];
        for (NSDictionary *dict in tempArray) {
            TZLiveModel *model = [TZLiveModel LiveModelWithDict:dict];
            [mArray addObject:model];
        }
        _liveArray = mArray;
    }
    return _liveArray;
}
@end

